package siga.visao_cpa;

import javax.swing.JOptionPane;
import siga.banco.conexao;
import siga.visao_busca.buscaCpaGrupo;
import siga.visao_busca.buscaCpaQuestao;
import siga.visao_busca.buscaCpaSubgrupo;
import siga.visao.index;

/**
 *
 * @author rafaelfrasson
 */
public class cpa_ins_questao extends javax.swing.JDialog {
    public static boolean cpaiq_finaliza = false;
    public cpa_ins_questao(java.awt.Frame parent, boolean modal) {
        super(parent, modal);
        initComponents();
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jLabel1 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        jLabel3 = new javax.swing.JLabel();
        jLabel4 = new javax.swing.JLabel();
        jLabel5 = new javax.swing.JLabel();
        jtfCod_avaliacao = new javax.swing.JLabel();
        jtfDesc_avaliacao = new javax.swing.JLabel();
        jtfOrdem = new javax.swing.JTextField();
        jtfCod_grupo = new javax.swing.JTextField();
        jtfDesc_grupo = new javax.swing.JTextField();
        jtfCod_subgrupo = new javax.swing.JTextField();
        jtfDesc_subgrupo = new javax.swing.JTextField();
        jtfCod_questao = new javax.swing.JTextField();
        jtfDesc_questao = new javax.swing.JTextField();
        jButton1 = new javax.swing.JButton();
        jButton2 = new javax.swing.JButton();
        jbGrupo = new javax.swing.JButton();
        jbSubgrupo = new javax.swing.JButton();
        jbQuestoes = new javax.swing.JButton();
        jbCadastro = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setTitle("Inserir Questões - CPA");
        addWindowListener(new java.awt.event.WindowAdapter() {
            public void windowClosed(java.awt.event.WindowEvent evt) {
                formWindowClosed(evt);
            }
            public void windowOpened(java.awt.event.WindowEvent evt) {
                formWindowOpened(evt);
            }
        });

        jLabel1.setFont(new java.awt.Font("Tahoma", 1, 11));
        jLabel1.setText("Ordem:");
        jLabel1.setName("jLabel1"); // NOI18N

        jLabel2.setFont(new java.awt.Font("Tahoma", 1, 11));
        jLabel2.setText("Grupo:");
        jLabel2.setName("jLabel2"); // NOI18N

        jLabel3.setText("SubGrupo:");
        jLabel3.setName("jLabel3"); // NOI18N

        jLabel4.setFont(new java.awt.Font("Tahoma", 1, 11));
        jLabel4.setText("Questão:");
        jLabel4.setName("jLabel4"); // NOI18N

        jLabel5.setText("Avaliação:");
        jLabel5.setName("jLabel5"); // NOI18N

        jtfCod_avaliacao.setText("Codigo");
        jtfCod_avaliacao.setName("jtfCod_avaliacao"); // NOI18N

        jtfDesc_avaliacao.setText("Descricao");
        jtfDesc_avaliacao.setName("jtfDesc_avaliacao"); // NOI18N

        jtfOrdem.setName("jtfOrdem"); // NOI18N

        jtfCod_grupo.setEnabled(false);
        jtfCod_grupo.setName("jtfCod_grupo"); // NOI18N

        jtfDesc_grupo.setEnabled(false);
        jtfDesc_grupo.setName("jtfDesc_grupo"); // NOI18N

        jtfCod_subgrupo.setEnabled(false);
        jtfCod_subgrupo.setName("jtfCod_subgrupo"); // NOI18N

        jtfDesc_subgrupo.setEnabled(false);
        jtfDesc_subgrupo.setName("jtfDesc_subgrupo"); // NOI18N

        jtfCod_questao.setEnabled(false);
        jtfCod_questao.setName("jtfCod_questao"); // NOI18N

        jtfDesc_questao.setEnabled(false);
        jtfDesc_questao.setName("jtfDesc_questao"); // NOI18N

        jButton1.setIcon(new javax.swing.ImageIcon(getClass().getResource("/siga/imagem/salvar_32x32.png"))); // NOI18N
        jButton1.setToolTipText("Salvar");
        jButton1.setName("jButton1"); // NOI18N
        jButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton1ActionPerformed(evt);
            }
        });

        jButton2.setIcon(new javax.swing.ImageIcon(getClass().getResource("/siga/imagem/sair_32x32.png"))); // NOI18N
        jButton2.setToolTipText("Concluir");
        jButton2.setName("jButton2"); // NOI18N
        jButton2.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton2ActionPerformed(evt);
            }
        });

        jbGrupo.setIcon(new javax.swing.ImageIcon(getClass().getResource("/siga/imagem/busca_16x16.png"))); // NOI18N
        jbGrupo.setName("jbGrupo"); // NOI18N
        jbGrupo.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jbGrupoActionPerformed(evt);
            }
        });

        jbSubgrupo.setIcon(new javax.swing.ImageIcon(getClass().getResource("/siga/imagem/busca_16x16.png"))); // NOI18N
        jbSubgrupo.setName("jbSubgrupo"); // NOI18N
        jbSubgrupo.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jbSubgrupoActionPerformed(evt);
            }
        });

        jbQuestoes.setIcon(new javax.swing.ImageIcon(getClass().getResource("/siga/imagem/busca_16x16.png"))); // NOI18N
        jbQuestoes.setName("jbQuestoes"); // NOI18N
        jbQuestoes.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jbQuestoesActionPerformed(evt);
            }
        });

        jbCadastro.setIcon(new javax.swing.ImageIcon(getClass().getResource("/siga/imagem/page_add.png"))); // NOI18N
        jbCadastro.setName("jbCadastro"); // NOI18N
        jbCadastro.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jbCadastroActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel5)
                    .addComponent(jLabel1)
                    .addComponent(jLabel2)
                    .addComponent(jLabel3)
                    .addComponent(jLabel4)
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jtfCod_grupo, javax.swing.GroupLayout.DEFAULT_SIZE, 59, Short.MAX_VALUE)
                            .addComponent(jtfCod_subgrupo, javax.swing.GroupLayout.DEFAULT_SIZE, 59, Short.MAX_VALUE)
                            .addComponent(jtfCod_questao, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, 59, Short.MAX_VALUE))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(layout.createSequentialGroup()
                                .addComponent(jtfDesc_grupo, javax.swing.GroupLayout.PREFERRED_SIZE, 422, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                                .addComponent(jbGrupo, javax.swing.GroupLayout.PREFERRED_SIZE, 25, javax.swing.GroupLayout.PREFERRED_SIZE))
                            .addGroup(layout.createSequentialGroup()
                                .addComponent(jtfDesc_subgrupo, javax.swing.GroupLayout.PREFERRED_SIZE, 422, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                                .addComponent(jbSubgrupo, javax.swing.GroupLayout.PREFERRED_SIZE, 25, javax.swing.GroupLayout.PREFERRED_SIZE))
                            .addGroup(layout.createSequentialGroup()
                                .addComponent(jtfDesc_questao, javax.swing.GroupLayout.PREFERRED_SIZE, 422, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                                .addComponent(jbQuestoes, javax.swing.GroupLayout.PREFERRED_SIZE, 25, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(jbCadastro, javax.swing.GroupLayout.PREFERRED_SIZE, 25, javax.swing.GroupLayout.PREFERRED_SIZE))))
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                        .addComponent(jtfOrdem, javax.swing.GroupLayout.Alignment.LEADING)
                        .addGroup(javax.swing.GroupLayout.Alignment.LEADING, layout.createSequentialGroup()
                            .addComponent(jtfCod_avaliacao)
                            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                            .addComponent(jtfDesc_avaliacao)))
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                        .addComponent(jButton1)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jButton2)))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jLabel5)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jtfCod_avaliacao)
                    .addComponent(jtfDesc_avaliacao))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(jLabel1)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jtfOrdem, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jLabel2)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jtfCod_grupo, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addComponent(jtfDesc_grupo, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jbGrupo))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jLabel3)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jtfCod_subgrupo, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addComponent(jtfDesc_subgrupo, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jbSubgrupo))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jLabel4)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jtfCod_questao, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addComponent(jbCadastro)
                    .addComponent(jtfDesc_questao, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jbQuestoes))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jButton1)
                    .addComponent(jButton2))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    @SuppressWarnings("static-access")
    private void jbGrupoActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jbGrupoActionPerformed
        buscaCpaGrupo bcg = new buscaCpaGrupo(null,true);
        bcg.setLocation((index.tela.width-bcg.getWidth())/2, (index.tela.height-bcg.getHeight())/2);
        bcg.setVisible(true);
        if(bcg.bcpag_finaliza){
            jtfCod_grupo.setText(bcg.bcpag_dados.get(0).toString());
            jtfDesc_grupo.setText(bcg.bcpag_dados.get(1).toString());
            cpa_cad_questoes.cpaq_dados.add(2,bcg.bcpag_dados.get(0).toString());
            cpa_cad_questoes.cpaq_dados.add(3,bcg.bcpag_dados.get(1).toString());
            bcg.bcpag_dados.clear();
        }
    }//GEN-LAST:event_jbGrupoActionPerformed

    @SuppressWarnings("static-access")
    private void jbSubgrupoActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jbSubgrupoActionPerformed
        if(!jtfCod_grupo.getText().equals("")){
            buscaCpaSubgrupo bcs = new buscaCpaSubgrupo(null,true);
            bcs.setLocation((index.tela.width-bcs.getWidth())/2, (index.tela.height-bcs.getHeight())/2);
            bcs.setVisible(true);
            if(bcs.bcpasg_finaliza){
                jtfCod_subgrupo.setText(bcs.bcpasg_dados.get(0).toString());
                jtfDesc_subgrupo.setText(bcs.bcpasg_dados.get(1).toString());
                bcs.bcpasg_dados.clear();
            }
        }else{
            JOptionPane.showMessageDialog(null, "Favor selecionar um Grupo.","Alerta do Sistema", JOptionPane.INFORMATION_MESSAGE);
        }
    }//GEN-LAST:event_jbSubgrupoActionPerformed

    @SuppressWarnings("static-access")
    private void jbQuestoesActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jbQuestoesActionPerformed
        if(!jtfCod_grupo.getText().equals("")){
            buscaCpaQuestao bcq = new buscaCpaQuestao(null,true);
            bcq.setLocation((index.tela.width-bcq.getWidth())/2, (index.tela.height-bcq.getHeight())/2);
            bcq.setVisible(true);
            if(bcq.bcpaq_finaliza){
                jtfCod_questao.setText(bcq.bcpaq_dados.get(0).toString());
                jtfDesc_questao.setText(bcq.bcpaq_dados.get(1).toString());
                bcq.bcpaq_dados.clear();
            }
        }else{
            JOptionPane.showMessageDialog(null, "Favor selecionar um Grupo.","Alerta do Sistema", JOptionPane.INFORMATION_MESSAGE);
        }
    }//GEN-LAST:event_jbQuestoesActionPerformed

    private void jbCadastroActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jbCadastroActionPerformed
        cpa_cad_questoes_new cqn = new cpa_cad_questoes_new(null,true);
        cqn.setLocation((index.tela.width-cqn.getWidth())/2, (index.tela.height-cqn.getHeight())/2);
        cqn.setVisible(true);
    }//GEN-LAST:event_jbCadastroActionPerformed

    private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton2ActionPerformed
        cpaiq_finaliza = true;
        dispose();
    }//GEN-LAST:event_jButton2ActionPerformed

    private void formWindowOpened(java.awt.event.WindowEvent evt) {//GEN-FIRST:event_formWindowOpened
        jtfCod_avaliacao.setText(cpa_cad_questoes.cpaq_dados.get(0).toString());
        jtfDesc_avaliacao.setText(cpa_cad_questoes.cpaq_dados.get(1).toString());
    }//GEN-LAST:event_formWindowOpened

    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed
        if(!jtfCod_avaliacao.getText().equals("Codigo")&&!jtfCod_grupo.getText().equals("")&&!jtfCod_questao.getText().equals("")&&!jtfOrdem.getText().equals("")){
            String sub = (jtfCod_subgrupo.getText().equals(""))?"null":jtfCod_subgrupo.getText();
            conexao.Inserir("update cpa_questoes set cod_grupo="+jtfCod_grupo.getText()+",cod_subgrupo="+sub+",ordem="+jtfOrdem.getText()+" " +
                            " where codigo="+jtfCod_questao.getText()+" and cod_emp="+index.cod_emp+" and cod_avaliacao="+jtfCod_avaliacao.getText()+"");
            LimpaCampos();
        }else{
            JOptionPane.showMessageDialog(null, "Favor preencher todos os capos em negrito.","Alerta do Sistema",JOptionPane.INFORMATION_MESSAGE);
        }
    }//GEN-LAST:event_jButton1ActionPerformed

    private void formWindowClosed(java.awt.event.WindowEvent evt) {//GEN-FIRST:event_formWindowClosed
        cpaiq_finaliza = true;
    }//GEN-LAST:event_formWindowClosed
    private void LimpaCampos(){
        jtfCod_grupo.setText("");
        jtfDesc_grupo.setText("");
        jtfCod_questao.setText("");
        jtfDesc_questao.setText("");
        jtfOrdem.setText("");
        jtfCod_subgrupo.setText("");
        jtfDesc_subgrupo.setText("");
    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton jButton1;
    private javax.swing.JButton jButton2;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JButton jbCadastro;
    private javax.swing.JButton jbGrupo;
    private javax.swing.JButton jbQuestoes;
    private javax.swing.JButton jbSubgrupo;
    private javax.swing.JLabel jtfCod_avaliacao;
    private javax.swing.JTextField jtfCod_grupo;
    private javax.swing.JTextField jtfCod_questao;
    private javax.swing.JTextField jtfCod_subgrupo;
    private javax.swing.JLabel jtfDesc_avaliacao;
    private javax.swing.JTextField jtfDesc_grupo;
    private javax.swing.JTextField jtfDesc_questao;
    private javax.swing.JTextField jtfDesc_subgrupo;
    private javax.swing.JTextField jtfOrdem;
    // End of variables declaration//GEN-END:variables

}
